package com.softWang.system.controller;

import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.system.constants.SysModel;
import com.softWang.system.controller.vo.systemTenant.*;
import com.softWang.system.po.SystemUsers;
import com.softWang.system.service.SystemUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.system.po.SystemTenant;
import com.softWang.system.service.SystemTenantService;

@Api(value="租户管理",tags="租户管理")
@Validated
@RestController
@RequestMapping("/system/systemTenant")
public class SystemTenantController {
     @Resource
     private SystemTenantService systemTenantService;
     @Resource
     private SystemUsersService systemUsersService;

     @PreAuthorize("@ss.hasPermi('system:tenant:query')")
     @ApiOperation(value="列表(分页)")
     @RequestMapping(value = "/query", method = RequestMethod.POST)
     public ResponseResult<PageBean<SystemTenantQueryAllResult>> queryall(@RequestBody @Validated SystemTenantQueryAllParam param, HttpServletRequest request) {
         List<SystemTenantQueryAllResult> results=systemTenantService.queryall(param);
         PageBean page=new PageBean(results);
         return ResponseResult.success(page);
     }

     @PreAuthorize("@ss.hasPermi('system:tenant:add')")
     @ApiOperation(value="新增")
     @SysOperationLog(value = "新增租户", model = SysModel.TENANT_MANAGE)
     @RequestMapping(value = "/add", method = RequestMethod.POST)
     public ResponseResult<Long> add(@RequestBody @Validated SystemTenantAddParam param, HttpServletRequest request) throws Exception {
         Long id = systemTenantService.add(param);
         return ResponseResult.success(id);
     }

     @PreAuthorize("@ss.hasPermi('system:tenant:edit')")
     @ApiOperation(value="修改")
     //@SysOperationLog(value = "修改", model = SysModel.TENANT_MANAGE)
     @RequestMapping(value = "/edit", method = RequestMethod.POST)
     public ResponseResult<Long> edit(@RequestBody @Validated SystemTenantEditParam param, HttpServletRequest request) {
         Long id = systemTenantService.edit(param);
         return ResponseResult.success(id);
     }

     @PreAuthorize("@ss.hasPermi('system:tenant:detail')")
     @ApiOperation(value="查看详情")
     @RequestMapping(value = "/detail", method = RequestMethod.POST)
     public ResponseResult<SystemTenantDetailResult> detail(@RequestBody @Validated Idparam idparam) {
         SystemTenantDetailResult result = new SystemTenantDetailResult();
         SystemUsers user = systemUsersService.getTenantAdminByTenantId(idparam.getId());
         BeanUtils.copyProperties(user, result);
         SystemTenant entity = systemTenantService.getById(idparam.getId());
         BeanUtils.copyProperties(entity, result);
         return ResponseResult.success(result);
     }

     @PreAuthorize("@ss.hasPermi('system:tenant:del')")
     @ApiOperation(value="删除")
     @SysOperationLog(value = "删除", model = SysModel.TENANT_MANAGE)
     @RequestMapping(value = "/del", method = RequestMethod.POST)
     public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids, HttpServletRequest request) {
         systemTenantService.del(ids);
         return ResponseResult.success();
     }

    @PreAuthorize("@ss.hasPermi('system:tenant:reset')")
    @ApiOperation(value="重置租户密码")
    @SysOperationLog(value = "重置租户密码", model = SysModel.TENANT_MANAGE)
    @RequestMapping(value = "/initPassword", method = RequestMethod.POST)
    public ResponseResult initPassword(@RequestBody @Validated Idparam idparam) {
        systemTenantService.initPassword(idparam.getId());
        return ResponseResult.success();
    }

    @PreAuthorize("@ss.hasPermi('system:tenant:menu')")
    @ApiOperation(value="租户分配菜单")
    @SysOperationLog(value = "租户分配菜单", model = SysModel.TENANT_MANAGE)
    @RequestMapping(value = "/assignMenu", method = RequestMethod.POST)
    public ResponseResult assignMenu(@RequestBody @Validated SystemTenantAssignMenuParam param) {
        systemTenantService.assignMenu(param);
        return ResponseResult.success();
    }

    @ApiOperation(value="查询租户菜单")
    @RequestMapping(value = "/getTenantMenu", method = RequestMethod.POST)
    public ResponseResult<SystemTenantGetTenantMenuResult> getTenantMenu(@RequestBody @Validated Idparam idparam) {
        SystemTenantGetTenantMenuResult result =  systemTenantService.getTenantMenu(idparam.getId());
        return ResponseResult.success(result);
    }
}